package com.jiegui.socket;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.RandomUtil;
import com.alibaba.fastjson.JSON;
import com.jiegui.common.utils.Crc16Utils;
import com.jiegui.common.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @description: socket消息解析
 * @author: wuqicheng
 * @time: 16/2/2023 下午 2:19
 */

public class SocketReadUtil {
    private static final Logger logger = LoggerFactory.getLogger("file-socket");

    public static void distribute(String str, String ip) {
        // logger.info("接收到的原消息为：" + str);

        //  str = str.replaceAll("7d 01", "7d").replaceAll("7d 02", "7d");
        logger.info("接收到IP为{},消息体为{}:", ip, str);
        String[] result = null;
        String[] cmd = str.split(" ");
        switch (cmd[5].toLowerCase()) {
            //登录认证
            case "01":
                result = cmd02(cmd);
                SocketSendUtil.sendMsg(result, ip);//登录认证应答
                break;
            case "03":
                result = cmd04(cmd);
                SocketSendUtil.sendMsg(result, ip);//8 心跳请求应答
                break;
            case "05":
                result = cmd06(cmd);
                SocketSendUtil.sendMsg(result, ip);//计费模型验证请求应答
                break;
            case "09":
                result = cmd0a(cmd);
                SocketSendUtil.sendMsg(result, ip);//8 计费模型请求应答
                break;

            case "31":
                logger.info("刷卡");
                result = cmd32(cmd);
                SocketSendUtil.sendMsg(result, ip);//8 计费模型请求应答
                logger.info("刷卡反馈");
                break;
        }

    }

    /**
     * @param cmd:
     * @return java.lang.String[]
     * @Author wuqicheng
     * @Description 登录认证应答
     * @Date 下午 5:58 18/3/2024
     **/
    public static String[] cmd02(String[] cmd) {

        String[] str = {"68", "0C", "00", "00", "00", "02", "1", "2", "3", "4", "5", "6", "7", "00", "xx", "xx"};
        str[6] = cmd[6];
        str[7] = cmd[7];
        str[8] = cmd[8];
        str[9] = cmd[9];
        str[10] = cmd[10];
        str[11] = cmd[11];
        str[12] = cmd[12];
        str = crc2(str);
        return str;
    }

    /**
     * @param cmd:
     * @return java.lang.String[]
     * @Author wuqicheng
     * @Description 心跳包应答
     * @Date 下午 5:58 18/3/2024
     **/
    public static String[] cmd04(String[] cmd) {

        String[] str = {"68", "0d", "36", "00", "00", "04", "1", "2", "3", "4", "5", "6", "7", "01", "00", "xx", "xx"};
        str[6] = cmd[6];
        str[7] = cmd[7];
        str[8] = cmd[8];
        str[9] = cmd[9];
        str[10] = cmd[10];
        str[11] = cmd[11];
        str[12] = cmd[12];
        str = crc2(str);
        return str;
    }

    /**
     * @param cmd:
     * @return java.lang.String[]
     * @Author wuqicheng
     * @Description 计费模型验证请求应答
     * @Date 下午 5:58 18/3/2024
     **/
    public static String[] cmd06(String[] cmd) {

        String[] str = {"68", "0e", "ce", "04", "00", "06", "1", "2", "3", "4", "5", "6", "7", "00", "00", "00", "xx", "xx"};
        str[6] = cmd[6];
        str[7] = cmd[7];
        str[8] = cmd[8];
        str[9] = cmd[9];
        str[10] = cmd[10];
        str[11] = cmd[11];
        str[12] = cmd[12];
        str = crc2(str);
        return str;
    }


    /**
     * @param cmd:
     * @return java.lang.String[]
     * @Author wuqicheng
     * @Description 计费模型请求应答
     * @Date 下午 5:58 18/3/2024
     **/
    public static String[] cmd0a(String[] cmd) {

        String[] str = {"68", "5E", "02", "00", "00", "0a", "1", "2", "3", "4", "5", "6", "7", "01", "00", "40", "0d", "03", "00", "9C", "40", "00", "00", "E0", "93", "04", "00"
                , "9C", "40", "00", "00", "80", "1A", "06", "00", "9C", "40", "00", "00", "20", "A1", "07", "00", "9C", "40", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00",
                "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00",
                "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "5E", "60"};
        str[6] = cmd[6];
        str[7] = cmd[7];
        str[8] = cmd[8];
        str[9] = cmd[9];
        str[10] = cmd[10];
        str[11] = cmd[11];
        str[12] = cmd[12];
        str = crc2(str);
        return str;
    }

    /**
     * @param cmd:
     * @return java.lang.String[]
     * @Author wuqicheng
     * @Description 运营平台确认启动充电
     * @Date 下午 5:58 18/3/2024
     **/
    public static String[] cmd32(String[] cmd) {
//                                                          6                              12         14                           21  22                       28
        String[] str = {"68", "2A", "00", "04", "00", "32", "1", "2", "3", "4", "5", "6", "7", "01", "1", "2", "3", "4", "5", "6", "7", "8", "1", "2", "3", "4", "5", "6", "7", "01",
                "00", "00", "00", "00", "00", "00", "00", "00", "A0", "86", "01", "00", "01", "00", "xx", "xx"};
        str[6] = cmd[6];
        str[7] = cmd[7];
        str[8] = cmd[8];
        str[9] = cmd[9];
        str[10] = cmd[10];
        str[11] = cmd[11];
        str[12] = cmd[12];

        str[13] = "01";
        str[14] = String.valueOf(DateUtil.year(new Date())).substring(2, 4);
        if (str[14].length() == 0) {
            str[14] = "0" + str[14];
        }
        str[15] = DateUtil.month(new Date()) + "";
        if (str[15].length() == 1) {
            str[15] = "0" + str[15];
        }
        str[16] = DateUtil.dayOfMonth(new Date()) + "";
        if (str[16].length() == 1) {
            str[16] = "0" + str[16];
        }
        str[17] = DateUtil.hour(new Date(), true) + "";
        if (str[17].length() == 1) {
            str[17] = "0" + str[17];
        }
        str[18] = DateUtil.minute(new Date()) + "";
        if (str[18].length() == 1) {
            str[18] = "0" + str[18];
        }
        str[19] = DateUtil.second(new Date()) + "";
        if (str[19].length() == 1) {
            str[19] = "0" + str[19];
        }

        str[20] = RandomUtil.randomInt(99) + "";
        if (str[20].length() == 1) {
            str[20] = "0" + str[20];
        }
        str[21] = RandomUtil.randomInt(99) + "";
        if (str[21].length() == 1) {
            str[21] = "0" + str[21];
        }


        str[22] = cmd[6];
        str[23] = cmd[7];
        str[24] = cmd[8];
        str[25] = cmd[9];
        str[26] = cmd[10];
        str[27] = cmd[11];
        str[28] = cmd[12];
        str = crc2(str);

        //16-23物理卡号
        return str;
    }

    /**
     * @param cmd: 1-7 设备号 7-11 卡号
     * @return java.lang.String[]
     * @Author wuqicheng
     * @Description 计费模型验证请求应答
     * @Date 下午 5:58 18/3/2024
     **/
    public static String[] cmd34(String[] cmd) {
        //                                                   6                             12        14                                 21  22                             28
        String[] str = {"68", "30", "00", "7c", "00", "34", "1", "2", "3", "4", "5", "6", "7","01", "1", "2", "3", "4", "5", "6", "7", "8", "1", "2", "3", "4", "5", "6", "7", "01",
                "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "1", "2", "3", "4", "A0", "86", "01", "00", "xx", "xx"};
        str[6] = cmd[0];
        str[7] = cmd[1];
        str[8] = cmd[2];
        str[9] = cmd[3];
        str[10] = cmd[4];
        str[11] = cmd[5];
        str[12] = cmd[6];

        str[14] = String.valueOf(DateUtil.year(new Date())).substring(2, 4);
        if (str[14].length() == 0) {
            str[14] = "0" + str[14];
        }
        str[15] = DateUtil.month(new Date()) + "";
        if (str[15].length() == 1) {
            str[15] = "0" + str[15];
        }
        str[16] = DateUtil.dayOfMonth(new Date()) + "";
        if (str[16].length() == 1) {
            str[16] = "0" + str[16];
        }
        str[17] = DateUtil.hour(new Date(), true) + "";
        if (str[17].length() == 1) {
            str[17] = "0" + str[17];
        }
        str[18] = DateUtil.minute(new Date()) + "";
        if (str[18].length() == 1) {
            str[18] = "0" + str[18];
        }
        str[19] = DateUtil.second(new Date()) + "";
        if (str[19].length() == 1) {
            str[19] = "0" + str[19];
        }

        str[20] = RandomUtil.randomInt(99) + "";
        if (str[20].length() == 1) {
            str[20] = "0" + str[20];
        }
        str[21] = RandomUtil.randomInt(99) + "";
        if (str[21].length() == 1) {
            str[21] = "0" + str[21];
        }

        str[22] = cmd[0];
        str[23] = cmd[1];
        str[24] = cmd[2];
        str[25] = cmd[3];
        str[26] = cmd[4];
        str[27] = cmd[5];
        str[28] = cmd[6];
        str[42]=cmd[7];
        str[43]=cmd[8];
        str[44]=cmd[9];
        str[45]=cmd[10];

        str = crc2(str);
        return str;
    }

    /**
     * @param cmd:
     * @return java.lang.String[]
     * @Author wuqicheng
     * @Description 运营平台远程停机
     * @Date 下午 5:58 18/3/2024
     **/
    public static String[] cmd36(String[] cmd) {

        String[] str = {"68", "0c", "00", "03", "00", "36", "1", "2", "3", "4", "5", "6", "7", "01",  "xx", "xx"};
        str[6] = cmd[0];
        str[7] = cmd[1];
        str[8] = cmd[2];
        str[9] = cmd[3];
        str[10] = cmd[4];
        str[11] = cmd[5];
        str[12] = cmd[6];
        str = crc2(str);
        return str;
    }

    public static String[] crc2(String[] str) {
        try {
            String[] newArray = Arrays.copyOfRange(str, 2, str.length - 2); // [3, 4]
            StringBuffer sb = new StringBuffer();
            for (String s : newArray) {
                sb.append(s);
            }
            List crc2 = Crc16Utils.getCRC2(sb.toString());
            str[str.length - 2] =  String.valueOf(crc2.get(0)).length()==1?("0"+String.valueOf(crc2.get(0))):String.valueOf(crc2.get(0));
            str[str.length - 1] = String.valueOf(crc2.get(1)).length()==1?("0"+String.valueOf(crc2.get(1))):String.valueOf(crc2.get(1));

            System.out.println(JSON.toJSONString(str));

        } catch (Exception e) {
            e.printStackTrace();
        }
        return str;
    }

}
